﻿// ===============================================================================
// XCI.NET 西安交通信息WinForm开发框架 2018 版权所有
// ===============================================================================
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraTreeList.Columns;
using XCI.Core;
using XCI.Extensions;
using XCI.Windows;
using XCI.Helper;

namespace XCI.Setting.Controls
{
    [ToolboxItem(false)]
    public partial class SettingColumns : BaseUserControl
    {
        public SettingColumns()
        {
            InitializeComponent();
        }

        public void Init()
        {
            LoadFiledName();
            LoadData();
        }

        [Browsable(false), DefaultValue(null), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public Func<ApiModel> GetModelInfo { get; set; }
        [Browsable(false), DefaultValue(null), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public Func<IList> GetColumns { get; set; }
        [Browsable(false), DefaultValue(null), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public Action AddColumn { get; set; }
        [Browsable(false), DefaultValue(null), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public Action<object> RemoveColumn { get; set; }
        [Browsable(false), DefaultValue(null), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public Action AutoBuildColumn { get; set; }

        protected void LoadFiledName()
        {
            var modelInfo = GetModelInfo?.Invoke();
            if (modelInfo == null) return;

            comboxFieldName.Items.BeginUpdate();
            comboxCaption.Items.BeginUpdate();

            comboxFieldName.Items.Clear();
            comboxCaption.Items.Clear();
            foreach (var item in modelInfo.Propertys)
            {
                comboxFieldName.Items.Add(item.Name);
                if (!string.IsNullOrEmpty(item.Caption))
                {
                    comboxCaption.Items.Add(item.Caption);
                }
            }
            comboxFieldName.Items.EndUpdate();
            comboxCaption.Items.EndUpdate();
        }

        protected void LoadData()
        {
            gridControl1.DataSource = GetColumns?.Invoke();
        }

        private void SetObject()
        {
            var obj = gridView1.GetRow(gridView1.FocusedRowHandle);
            if (obj == null) return;
            proGridColumn.SelectedObject = obj;

            if (obj is GridColumn gridColumn)
            {
                proGridOptionsColumn.SelectedObject = gridColumn.OptionsColumn;
                proGridColumnHeader.SelectedObject = gridColumn.AppearanceHeader;
                proGridAppearanceCell.SelectedObject = gridColumn.AppearanceCell;
            }
            else if (obj is TreeListColumn treeListColumn)
            {
                proGridOptionsColumn.SelectedObject = treeListColumn.OptionsColumn;
                proGridColumnHeader.SelectedObject = treeListColumn.AppearanceHeader;
                proGridAppearanceCell.SelectedObject = treeListColumn.AppearanceCell;
            }
        }

        private void gridViewGridColumn_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            SetObject();
            btnDelete.Enabled = gridView1.FocusedRowHandle > -1;
        }

        //private void gridView1_CustomDrawRowIndicator(object sender, RowIndicatorCustomDrawEventArgs e)
        //{
        //    if (gridView1.OptionsView.ShowIndicator && e.Info.IsRowIndicator && e.RowHandle >= 0)
        //    {
        //        e.Info.DisplayText = (e.RowHandle + 1).ToString();
        //        e.Info.ImageIndex = -1;
        //    }
        //}

        private void btnNew_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            AddColumn?.Invoke();
            LoadData();
            if (GetColumns != null)
            {
                List<GridColumn> gridColumns = new List<GridColumn>();
                List<TreeListColumn> treeColumns = new List<TreeListColumn>();
                foreach (var item in GetColumns())
                {
                    if(item is GridColumn column) gridColumns.Add(column);
                    else if (item is TreeListColumn listColumn) treeColumns.Add(listColumn);
                }

                if (gridColumns.Count > 0)
                {
                    gridView1.MoveBy(gridColumns.Count(p => p.Visible) - 1);
                }
                else if (treeColumns.Count > 0)
                {
                    gridView1.MoveBy(treeColumns.Count(p => p.Visible) - 1);
                }
                else
                {
                    gridView1.MoveLastVisible();
                }
            }
        }

        private void btnDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (MessageBoxHelper.ShowWarnYesNo("确定要删除选中的列吗？") != DialogResult.Yes) return;
            var rows = new List<object>();
            foreach (var rowIndex in gridView1.GetSelectedRows())
            {
                rows.Add(gridView1.GetRow(rowIndex));
            }
            foreach (var row in rows)
            {
                if (row == null) continue;
                RemoveColumn?.Invoke(row);
            }
            LoadData();
        }

        private void btnAutoGenerated_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (MessageBoxHelper.ShowMessageYesNo("当前列将被清除,确定自动生成列吗？") != DialogResult.Yes) return;
            AutoBuildColumn?.Invoke();
            LoadData();
        }

        private void GridView1_CustomDrawRowIndicator(object sender, RowIndicatorCustomDrawEventArgs e)
        {
            // if (!e.Info.IsRowIndicator || e.RowHandle < 0) return;
            // var numbers = e.RowHandle + 1;
            // e.Info.DisplayText = numbers.ToString();
            // e.Info.ImageIndex = -1;
        }
    }
}